package com.yonyou.pmclouds.projecttype.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.mapper.ProjectTypeMapper;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeQuery;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectTypeQuery.class)
public class ProjectTypeQueryImpl implements ProjectTypeQuery {
    @Autowired
    private ProjectTypeMapper mapper;
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;
    @Autowired
    private ProjectTypeQuery projectTypeQuery;
    @Override
    public ProjectTypeVO queryById(String id) throws BusinessException {
        return mapper.getById(id);
    }

    @Override
    public ProjectTypeVO[] queryByIds(String[] ids) throws BusinessException {
        ProjectTypeVO[] projectTypeVOS = mapper.queryNamesByIds(ids);
        return projectTypeVOS;
    }

    @Override
    public ProjectTypeVO queryByPkSrc(String pkSrc) throws BusinessException {
        String tenantid = RuntimeEnvironment.getTenantId();
        ProjectTypeVO[] projectTypeVOs = mapper.queryVOBySrcIds(tenantid,new String[]{pkSrc});
        if(ArrayUtils.isEmpty(projectTypeVOs))
            return null;
        return projectTypeVOs[0];
    }

    @Override
    public Map<String, String> queryNamesByIds(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids))
            return new HashMap<>();
        ProjectTypeVO[] typeVOS = mapper.queryNamesByIds(ids);
        if(ArrayUtils.isEmpty(typeVOS))
            return new HashMap<>();
        Map<String, String> result = new HashMap<>(typeVOS.length);
        for (ProjectTypeVO vo : typeVOS
        ) {
            result.put(vo.getPkProjectType(), vo.getSname());
        }
        return result;
    }

    @Override
    public String queryNameById(String id) throws BusinessException {
        return mapper.queryNameById(id);
    }

    @Override
    public ProjectTypeVO[] queryList(String pkTenant, byte enableStatus) {
        ProjectTypeVO[] projectTypeVOS = mapper.queryList(pkTenant, enableStatus);
        return projectTypeVOS;
    }

    @Override
    public ProjectTypeVO[] queryByOrg(String pkOrg) throws BusinessException {
        String tenantId = RuntimeEnvironment.getTenantId();
        List<ProjectVO> projectVOS = projectSimpleQuery.queryProjectsByTypeAndOrgs(tenantId, pkOrg, null, null);
        Set<String> typeIds=new HashSet<>();
        if (projectVOS==null||projectVOS.size()==0){
            return new ProjectTypeVO[0];
        }
        for (ProjectVO projectVO:projectVOS){
            typeIds.add(projectVO.getPkProjectType());
        }
        if (typeIds==null||typeIds.size()==0){
            return new ProjectTypeVO[0];
        }
        ProjectTypeVO[] projectTypeVOS = projectTypeQuery.queryByIds(typeIds.toArray(new String[0]));
        return projectTypeVOS;
    }
}
